package vn.com.shoppie.view;

import vn.com.shoppie.sobject.Slider;
import vn.com.shoppie.util.BitmapWorkerTask;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

@SuppressLint("ValidFragment")
public final class SlideFragment extends Fragment implements BitmapWorkerTask.OnLoadBitmapHandle{
    private static final String KEY_CONTENT = "TestFragment:Content";
//    private static final String KEY_TITLE = "TestFragment:Title";
    SlideHolder holder=new SlideHolder();
    public SlideFragment(Slider slider) {
    	this.mShop=slider;
    }
    public static SlideFragment newInstance(Slider slider) {
        SlideFragment fragment = new SlideFragment(slider);
        
        return fragment;
    }

    private Slider mShop = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mShop.slideDesc = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		holder.imageView = new ImageView(SlideFragment.this.getActivity());
		holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		holder.imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		BitmapWorkerTask.loadBitmap(getActivity(),this, holder, mShop);
        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.setGravity(Gravity.CENTER);
        layout.addView(holder.imageView);

        return layout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mShop.slideDesc);
    }
    public class SlideHolder extends BitmapWorkerTask.BitmapHolder{
    	ImageView imageView;
    	Bitmap bmp;
		@Override
		public ImageView getImageView() {
			return imageView;
		}
		@Override
		public Bitmap getBitmapHolder() {
			return bmp;
		}
    }
	@Override
	public Bitmap loadBitmapInBackground(Object... strings) {
		if(strings[0] instanceof Slider){
			Slider slide=(Slider)strings[0];
			return slide.getBitmap(getActivity(), slide.slideImage);
		}
		return null;
	}
}
